﻿using System;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.ComponentModelHost;
using Microsoft.VisualStudio.Shell;
using Constants = Red.Scripts.Defines.Constants;

namespace Red.Scripts
{
    /// <summary>
    /// This class implements a Visual Studio package that is registered for the Visual Studio IDE.
    /// The package class uses a number of registration attributes to specify integration parameters.
    /// </summary>
    [PackageRegistration( UseManagedResourcesOnly = true )]
    [ProvideProfile( typeof( Options.PageGeneral ), Constants.Options.Name, Constants.Options.General.Name, 100, 101, true, DescriptionResourceID = 100 )]
    [ProvideOptionPage( typeof( Options.PageGeneral ), Constants.Options.Name, Constants.Options.General.Name, 100, 101, true )]
    [ProvideProfile( typeof( Options.SccSearchOrder.Page ), Constants.Options.Name, Constants.Options.SccSearchOrder.Name, 100, 102, true, DescriptionResourceID = 100 )]
    [ProvideOptionPage( typeof( Options.SccSearchOrder.Page ), Constants.Options.Name, Constants.Options.SccSearchOrder.Name, 100, 102, true, SupportsProfiles = true )]
    [InstalledProductRegistration( "Red Scripts", "Fabulousness in script form", "1.0.0.0", IconResourceID = 500 )]
    [ProvideMenuResource( "Toolbar.ctmenu", 1 )]
    [ProvideToolWindow( typeof( Options.Tool.Pane.FindSymbol.Window ) )]
    [ProvideAutoLoad( Defines.Guids.Project.Raw )]
    [Guid( Defines.Guids.Package.Raw )]
    public partial class RedScriptsPackage : Package
    {
        /// <summary>
        /// Initialization of the package.  This is where you should put all initialization
        /// code that depends on VS services.
        /// </summary>
        protected override void Initialize()
        {
            Debug.Print( "Red Scripts Package Initialize()" );

            base.Initialize();
            // TODO: add initialization code here

            InitializeToolbar();
            InitializeOptions();

            InitializeLegacy();
        }

        private void InitializeOptions()
        {
            GetDialogPage( typeof( Options.PageGeneral ) );
            GetDialogPage( typeof( Options.SccSearchOrder.Page ) );
        }

        private void InitializeToolbar()
        {
            IComponentModel componentModel = (IComponentModel)GetService( typeof( SComponentModel ) );
            Options.Tool.Bar.Reparse reparse = componentModel.GetService<Options.Tool.Bar.Reparse>();
            Options.Tool.Bar.BuildAndContinue buildAndContinue = componentModel.GetService<Options.Tool.Bar.BuildAndContinue>();
            Options.Tool.Bar.ManageSccProcess manage = componentModel.GetService<Options.Tool.Bar.ManageSccProcess>();
            Options.Tool.Pane.FindSymbol.Command findSymbolCommand = componentModel.GetService<Options.Tool.Pane.FindSymbol.Command>();

            OleMenuCommandService commandService = (OleMenuCommandService)GetService( typeof( IMenuCommandService ) );
            reparse.Initialize( commandService );
            buildAndContinue.Initialize( commandService );
            manage.Initialize( commandService );
            findSymbolCommand.Initialize( commandService );
        }
    }
}
